package com.ellricky.baselib.ui.base;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.andr.common.http.callback.BaseCallback;
import com.andr.common.ui.adapter.BaseRecyclerAdapter;
import com.andr.common.ui.fragment.BaseToolBarFragment;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.widget.refresh.RefreshRvView;
import com.andr.common.widget.refresh.RvRefreshListener;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.ellricky.baselib.cache.LocalCacheUtil;
import com.ellricky.baselib.http.HttpUtils;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.util.NewEBaseCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseRefreshFragment<V, T extends BasePresenter<V>, E>
        extends BaseToolBarFragment<V, T> implements RvRefreshListener, AdapterView.OnItemClickListener {

    protected RefreshRvView rvView;
    protected BaseRecyclerAdapter<E> adapter;
    protected List<E> dataSource = new ArrayList<>();
    protected boolean refresh;
    protected Map<String, String> paramMap;

    @Override
    public void initUIView(View view) {
        super.initUIView(view);
        rvView = view.findViewById(R.id.rvView);
        setRefreshListener();

    }

    @Override
    public int getContentLayout() {
        return R.layout.common_layout_rv_refresh;
    }

    @Override
    public void onResume() {
        super.onResume();
        boolean load = LocalCacheUtil.isReloadData();
        if (load && !EAppUtil.repeatResume()) loadDataOnInit();
        LocalCacheUtil.setIsReloadData(true);
    }

    @Override
    public void setAdapter(RecyclerView rv) {
        adapter = getRvAdapter();
        adapter.setOnItemClickListener(this);
        rv.setAdapter(adapter);
    }

    @Override
    public void loadData(boolean refresh) {
        this.refresh = refresh;
        String url = EAppUtil.getApiHost() + getUrlPath();
        paramMap = new HashMap<>();
        initParamMap();
        NewEBaseCallback callback = getResponseCallback2();
        if (callback != null)
            HttpUtils.getInstance().postFrom(url, paramMap, callback);
    }

    @Override
    public void updateAdapterData(boolean refresh) {
        if (refresh) adapter.refresh(dataSource);
        else adapter.loadMore(dataSource);
    }

///////////////////////////////////////////////////////////////////////////
//  子类可重写的方法
///////////////////////////////////////////////////////////////////////////

    protected void setRefreshListener() {
        rvView.init(this);
    }

    public void loadDataOnInit() {
        rvView.loadContent(true);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        // click item
    }


    protected NewEBaseCallback getResponseCallback2() {
        return null;
    }

    protected abstract BaseCallback getResponseCallback();

    protected abstract BaseRecyclerAdapter<E> getRvAdapter();

    protected abstract String getUrlPath();

    protected void initParamMap() {
        paramMap.put("page", String.valueOf(rvView.getCurPage()));
        paramMap.put("page_size", "40");
    }

    protected void hideToolBar() {
        if (mToolbar != null) {
            mToolbar.setVisibility(View.GONE);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == BaseConstant.RESULT_CODE_ADD ||
                resultCode == BaseConstant.RESULT_CODE_DETAIL) {
            rvView.loadContent(true);
        }
    }
}
